﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TARLib
{
    public class Incasso
    {
        public Incasso(int? id, string descrizione, Decimal daIncassare, Decimal incassato, string provenienza, string note, string author_Insert, DateTime timeStamp_Insert, int? userIDPagante)
        {
            ID = id;
            Descrizione = descrizione;
            DaIncassare = daIncassare;
            Incassato = incassato;
            Provenienza = provenienza;
            Note = note;
            Author_Insert = author_Insert;
            TimeStamp_Insert = timeStamp_Insert;
            UserIDPagante = userIDPagante;
        }

        public int? ID { get; set; }
        public string Descrizione { get; set; }
        public Decimal DaIncassare { get; set; }
        public Decimal Incassato { get; set; }
        public string Provenienza { get; set; }
        public string Note { get; set; }
        public string Author_Insert { get; set; }
        public DateTime TimeStamp_Insert { get; set; }
        public int? UserIDPagante { get; set; }

        public void Salva()
        {
            SqlConnection conn = new SqlConnection(Database.GetDBConnectionString());
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "[Generale].[SP_Incassi_I]";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Descrizione", System.Data.SqlDbType.NVarChar, 8000) { Value = Descrizione });
            cmd.Parameters.Add(new SqlParameter("@DaIncassare", System.Data.SqlDbType.Decimal) { Value = DaIncassare, Precision = 10, Scale = 2 });
            cmd.Parameters.Add(new SqlParameter("@Incassato", System.Data.SqlDbType.Decimal) { Value = Incassato, Precision = 10, Scale = 2 });
            cmd.Parameters.Add(new SqlParameter("@Provenienza", System.Data.SqlDbType.NVarChar, 200) { Value = Provenienza });
            cmd.Parameters.Add(new SqlParameter("@Author_Insert", System.Data.SqlDbType.NVarChar, 256) { Value = Author_Insert });
            cmd.Parameters.Add(new SqlParameter("@TimeStamp_Insert", System.Data.SqlDbType.DateTime) { Value = TimeStamp_Insert });

            if (UserIDPagante.HasValue)
            {
                cmd.Parameters.Add(new SqlParameter("@UserIDPagante", System.Data.SqlDbType.Int) { Value = UserIDPagante });
            }
            else
            {
                cmd.Parameters.Add(new SqlParameter("@UserIDPagante", System.Data.SqlDbType.Int) { Value = DBNull.Value });
            }

            if (Note == null)
            {
                cmd.Parameters.Add(new SqlParameter("@Note", System.Data.SqlDbType.NVarChar, 8000) { Value = DBNull.Value });
            }
            else
            {
                cmd.Parameters.Add(new SqlParameter("@Note", System.Data.SqlDbType.NVarChar, 8000) { Value = Note });
            }

            cmd.Parameters.Add(new SqlParameter("@ID", System.Data.SqlDbType.Int, -1) { Direction = System.Data.ParameterDirection.Output });

            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
    }
}
